package matlap.client.view.student;

import matlap.client.services.student.StudentService;
import matlap.client.services.student.StudentServiceAsync;
import matlap.shared.Student;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class AccountComposite extends Composite {

	String userName = "";
	private VerticalPanel vp;
	StudentServiceAsync us = GWT.create(StudentService.class);
	private Label lbUser;
	private Label lbWho;
	private Label lbFirstName;
	private TextBox tbFirstName;
	private Label lbLastName;
	private TextBox tbLastName;
	private Label lbEmail;
	private TextBox tbEmail;
	private Label lbPhone;
	private TextBox tbPhone;
	private Label lbAdress;
	private TextBox tbAdress;
	private Button btnSave;
	private Label lblOsztly;
	private Label lbSchool;
	private TextBox tbSchool;
	private TextBox tbClass;
	private Label lbPassword;
	private TextBox tbPassword;

	public AccountComposite(String who) {
		userName = who;
		initPanel();
		us.getStudentByUserName(userName, new AsyncCallback<Student>() {

			public void onFailure(Throwable caught) {

			}

			public void onSuccess(Student result) {

				lbWho.setText(userName);
				tbFirstName.setText(result.getFirstName());
				tbLastName.setText(result.getLastName());
				tbEmail.setText(result.getEmail());
				tbAdress.setText(result.getAddress());
				tbPhone.setText(result.getPhone());
				tbClass.setText(String.valueOf(result.getClassNr()));
				tbSchool.setText(result.getSchool());
				tbPassword.setText(result.getPassword());

			}

		});

		btnSave.addClickHandler(new ClickHandler() {

			public void onClick(ClickEvent event) {
				Student user = new Student();
				user.setUserName(lbWho.getText());
				user.setFirstName(tbFirstName.getText());
				user.setLastName(tbLastName.getText());
				user.setEmail(tbEmail.getText());
				user.setAddress(tbAdress.getText());
				user.setPhone(tbPhone.getText());
				user.setClassNr(Integer.parseInt(tbClass.getText()));
				user.setSchool(tbSchool.getText());
				user.setPassword(tbPassword.getText());

				us.update(user, new AsyncCallback<Void>() {

					public void onFailure(Throwable caught) {
					}

					public void onSuccess(Void result) {
						Window.alert("Az adataid frissitve");
					}

				});

			}

		});
	}

	public void initPanel() {
		vp = new VerticalPanel();
		initWidget(vp);
		vp.setSize("313px", "319px");

		Label lbAccount = new Label("Adataim");
		lbAccount.setStyleName("BigBold");
		vp.add(lbAccount);
		vp.setCellHorizontalAlignment(lbAccount,
				HasHorizontalAlignment.ALIGN_CENTER);

		FlexTable accountFlex = new FlexTable();
		vp.add(accountFlex);

		lbUser = new Label("Felhaszn\u00E1l\u00F3");
		accountFlex.setWidget(0, 0, lbUser);

		lbWho = new Label("");
		lbWho.setStyleName("BigBold");
		accountFlex.setWidget(0, 1, lbWho);

		lbPassword = new Label("Jelsz\u00F3");
		accountFlex.setWidget(1, 0, lbPassword);

		tbPassword = new TextBox();
		accountFlex.setWidget(1, 1, tbPassword);

		lbLastName = new Label("Vezet\u00E9kn\u00E9v");
		accountFlex.setWidget(2, 0, lbLastName);

		tbLastName = new TextBox();
		accountFlex.setWidget(2, 1, tbLastName);

		lbFirstName = new Label("Keresztn\u00E9v");
		accountFlex.setWidget(3, 0, lbFirstName);

		tbFirstName = new TextBox();
		accountFlex.setWidget(3, 1, tbFirstName);

		lbSchool = new Label("Iskola");
		accountFlex.setWidget(4, 0, lbSchool);

		tbSchool = new TextBox();
		tbSchool.setText("");
		accountFlex.setWidget(4, 1, tbSchool);

		lblOsztly = new Label("Oszt\u00E1ly");
		accountFlex.setWidget(5, 0, lblOsztly);

		tbClass = new TextBox();
		accountFlex.setWidget(5, 1, tbClass);

		lbEmail = new Label("E-mail");
		accountFlex.setWidget(6, 0, lbEmail);

		tbEmail = new TextBox();
		accountFlex.setWidget(6, 1, tbEmail);

		lbPhone = new Label("Telefon");
		accountFlex.setWidget(7, 0, lbPhone);

		tbPhone = new TextBox();
		accountFlex.setWidget(7, 1, tbPhone);

		lbAdress = new Label("C\u00EDm");
		accountFlex.setWidget(8, 0, lbAdress);

		tbAdress = new TextBox();
		accountFlex.setWidget(8, 1, tbAdress);

		btnSave = new Button("Ment\u00E9s");
		accountFlex.setWidget(9, 1, btnSave);
	}

}
